package net.eearley.redpoint.app;

import java.util.Map;
import java.util.Set;

 
/**
 * The Class Inventory.
 */
public class Inventory {

	/** The map. */
	private Map<Denomination, Integer> map;

	/**
	 * Instantiates a new inventory.
	 * 
	 * @param map the map
	 */
	public Inventory(Map<Denomination, Integer> map) {
		this.map = map;
	}

	/**
	 * Find amount of bills.
	 * 
	 * @param denomination the denomination
	 * 
	 * @return the integer
	 */
	public Integer findAmountOfBills(Denomination denomination) {
		return map.get(denomination);
	}

	/**
	 * Find current value by denom.
	 * 
	 * @param denomination the denomination
	 * 
	 * @return the integer
	 */
	public Integer findCurrentValueByDenom(Denomination denomination) {
		return map.get(denomination) * denomination.getVal();
	}

	/**
	 * Gets the size.
	 * 
	 * @return the size
	 */
	public Integer getSize() {
		return map.size();
	}

	/**
	 * Gets the denoms.
	 * 
	 * @return the denoms
	 */
	public Set<Denomination> getDenoms() {
		return map.keySet();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer(map.size());
		Set<Map.Entry<Denomination, Integer>> set = map.entrySet();
		for (Map.Entry<Denomination, Integer> entry : set) {
			buffer.append(entry.getKey());
			buffer.append(",");
			buffer.append(entry.getValue());
			buffer.append('\n');
		}

		return buffer.toString();
	}

	/**
	 * Update amount of bills.
	 * 
	 * @param denomination the denomination
	 * @param amount the amount
	 */
	public void updateAmountOfBills(Denomination denomination, Integer amount) {
		map.remove(denomination);
		map.put(denomination, amount);
	}

	/**
	 * Gets the total amount.
	 * 
	 * @return the total amount
	 */
	public Integer getTotalAmount() {
		Integer total = 0;
		Set<Map.Entry<Denomination, Integer>> set = map.entrySet();
		for (Map.Entry<Denomination, Integer> entry : set) {
			total += entry.getKey().getVal() * entry.getValue();
		}
		return total;
	}

}
